package com.ktgj.customer.service.impl;

import com.ktgj.customer.domain.Bank;
import com.ktgj.customer.domain.ContactInformation;
import com.ktgj.customer.mapper.BankMapper;
import com.ktgj.customer.mapper.ContactlnformationMapper;
import com.ktgj.customer.service.IBankService;
import com.ktgj.customer.service.IContactlnformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 联系方式Service业务层处理
 */
@Service
public class ContactlnformationImpl implements IContactlnformationService
{
    @Autowired
    private ContactlnformationMapper contactlnformationMapper;

    /**
     * 后端——查询联系方式
     * */
    @Override
    public List<ContactInformation> backContactInformationById(Long customerId) throws Exception {
        if (customerId != null && customerId != 0){
            List<ContactInformation> contactInformations = contactlnformationMapper.backContactInformationById(customerId);
            List<ContactInformation> contactInformationList = new ArrayList<>();
            for (ContactInformation contactInformation : contactInformations) {
                if (contactInformation.getIsDeletes().equals("false")){
                    String type = contactInformationType(contactInformation.getContactInformationType());
                    contactInformation.setContactInformationType(type);
                    contactInformationList.add(contactInformation);
                }
            }
            if (contactInformations != null && contactInformations.size() > 0){
                return contactInformations;
            }
            else {
                throw new Exception("未查到数据");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——查询待修改联系方式
     * */
    @Override
    public ContactInformation backUpdateContactInformation(Long contactInformationId) throws Exception {
        if (contactInformationId != null && contactInformationId != 0){
            ContactInformation contactInformation = contactlnformationMapper.backUpdateContactInformation(contactInformationId);
            String type = contactInformationType(contactInformation.getContactInformationType());
            contactInformation.setContactInformationType(type);
            if (contactInformation != null){
                return contactInformation;
            }
            else {
                throw new Exception("未查到数据");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——修改联系方式
     * */
    @Override
    public int backUpdateContactInformationById(ContactInformation contactInformation) throws Exception {
        if (contactInformation != null){
            String name = contactInformationName(contactInformation.getContactInformationType());
            contactInformation.setContactInformationType(name);
            int i = contactlnformationMapper.backUpdateContactInformationById(contactInformation);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("修改失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——新增联系方式
     * */
    @Override
    public int backInsertContactInformationById(ContactInformation contactInformation) throws Exception {
        if (contactInformation != null){
            String name = contactInformationName(contactInformation.getContactInformationType());
            contactInformation.setIsDeletes("false");
            contactInformation.setContactInformationType(name);
            int i = contactlnformationMapper.backInsertContactInformationById(contactInformation);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("新增失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 后端——删除联系方式
     * */
    @Override
    public int backDeleteContactInformationById(Long contactInformationId) throws Exception {
        if (contactInformationId != null && contactInformationId != 0){
            int i = contactlnformationMapper.backDeleteContactInformationById(contactInformationId);
            if (i > 0){
                return 1;
            }
            else {
                throw new Exception("删除失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    public String contactInformationType(String contactInformationType){
        switch (contactInformationType){
            case "phone":
                contactInformationType = "手机";
                break;
            case "e-mail":
                contactInformationType = "电子邮件";
                break;
        }
        return contactInformationType;
    }

    public String contactInformationName(String contactInformationType){
        switch (contactInformationType){
            case "手机":
                contactInformationType = "phone";
                break;
            case "电子邮件":
                contactInformationType = "e-mail";
                break;
        }
        return contactInformationType;
    }
}
